package 淘宝爬虫;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import sun.misc.BASE64Encoder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZhuQianli on 2018/1/18.
 */
public class App3 {

    public static void main(String[] args) throws Exception {
        BASE64Encoder base64en = new BASE64Encoder();
        String newstr = base64en.encode("clorest510".getBytes());
        System.out.println(newstr);

        String s = sendGet("http://shop.m.taobao.com/shop/shopsearch/search_page_json.do?isb=&sort=default&type=all&loc=&fx=0&lp=0&jf=0&my=0&paytype=&_input_charset=utf-8&base64=1&q=Y2xvcmVzdDUxMA%3D%3D&shop_type=&olu=");
        System.out.println(s);

        String s3 = StringEscapeUtils.unescapeHtml4(s);
        System.out.println(s3);
    }

    /**
     * 字符串编码成Unicode编码
     */
    public static String encode(String src) throws Exception {
        char c;
        StringBuilder str = new StringBuilder();
        int intAsc;
        String strHex;
        for (int i = 0; i < src.length(); i++) {
            c = src.charAt(i);
            intAsc = (int) c;
            strHex = Integer.toHexString(intAsc);
            if (intAsc > 128)
                str.append("\\u" + strHex);
            else
                str.append("\\u00" + strHex); // 低位在前面补00
        }
        return str.toString();
    }

    /**
     * Unicode解码成字符串
     * @param src
     * @return
     */
    public static String decode(String src) {
        int t =  src.length() / 6;
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < t; i++) {
            String s = src.substring(i * 6, (i + 1) * 6); // 每6位描述一个字节
            // 高位需要补上00再转
            String s1 = s.substring(2, 4) + "00";
            // 低位直接转
            String s2 = s.substring(4);
            // 将16进制的string转为int
            int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);
            // 将int转换为字符
            char[] chars = Character.toChars(n);
            str.append(new String(chars));
        }
        return str.toString();
    }


    public static String sendGet(String url) throws IOException {
        CloseableHttpClient client = HttpClientBuilder.create().build();

        HttpGet httpGet = new HttpGet(url);

        Header[] headers = createHeaders();
        httpGet.setHeaders(headers);

        StringBuilder stringBuilder = new StringBuilder();
        try (CloseableHttpResponse execute = client.execute(httpGet)) {
            HttpEntity entity = execute.getEntity();
            InputStream content = entity.getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content, "GBK"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line).append("\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();


    }


    public static Header[] createHeaders() {
        List headersList = new ArrayList();
        headersList.add(new BasicHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"));
        headersList.add(new BasicHeader("accept-encoding", "gzip, deflate"));
        headersList.add(new BasicHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8"));
        headersList.add(new BasicHeader("cookie", ""));
        headersList.add(new BasicHeader("Host", "shop.m.taobao.com"));
        headersList.add(new BasicHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"));

        return (Header[]) headersList.toArray(new Header[headersList.size()]);
    }
}
